import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginPage from '../components/login-page.vue'
import HomePage from '../components/home-page.vue'
import WelcomPage from '../components/welcom-page.vue'
import UserPage from '../components/user/user-page.vue'
import RightsPage from '../components/power/rights-page.vue'
import RolesPage from '../components/power/roles-page.vue'
Vue.use(VueRouter, HomePage, WelcomPage, UserPage, RightsPage, RolesPage)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: LoginPage },
  {
    // 登入后自动跳转到 welcom 子组件
    path: '/home',
    component: HomePage,
    redirect: '/welcom',
    children: [
      {
        path: '/welcom',
        component: WelcomPage
      },
      {
        path: '/users',
        component: UserPage
      },
      {
        path: '/rights', component: RightsPage
      },
      {
        path: '/roles', component: RolesPage
      }]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // 用户直接访问登入 放行
  if (to.path === '/login') return next()
  // 获取 sessionstorage 中的 token 值
  const token = window.sessionStorage.getItem('token')
  // 如果 token 为空就跳转到登入页面
  if (!token) return next('/login')
  next()
})
export default router
/*
// 用户直接访问登入 放行
  if (to.path === '/login') return next()
  // 获取 sessionstorage 中的 token 值
  const token = window.sessionStorage.getItem('token')
  // 如果 token 为空就跳转到登入页面
  if (!token) return next('/login')
  next()
*/
